package com.ant.storage.controller;


import com.ant.storage.common.ResultPage;
import com.ant.storage.common.ResultVo;
import com.ant.storage.dto.DepDto;
import com.ant.storage.dto.PickingDto;
import com.ant.storage.pojo.*;
import com.ant.storage.service.ICarPickingDetailsService;
import com.ant.storage.service.ICarPickingService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.ServletContext;
import java.io.IOException;
import java.util.Date;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author melon
 * @since 2024-01-04
 */
@RestController
@RequestMapping("/car-picking")
public class CarPickingController {
    @Autowired
    private ICarPickingService pickingService;

    @Autowired
    private ICarPickingDetailsService pickingDetailsService;

    @Autowired
    public ServletContext servletContext;

    @ApiOperation(value = "获取领料数据列表")
    @GetMapping("/getPickingList")
    public ResultPage getPickingList(PickingDto dto) {
        return ResultPage.success("获取成功", pickingService.querylist(dto));
    }


    @ApiOperation(value = "保存领料单")
    @PostMapping("/savePicking")
    public ResultVo savePicking(@RequestBody CarPicking picking) throws IOException {
        return pickingService.savePicking(picking);
    }

    @ApiOperation(value = "修改领料单")
    @PostMapping("/pickingComplete")
    public ResultVo pickingComplete(@RequestBody  CarPicking picking) {
        return pickingService.pickingComplete(picking);
    }

    @ApiOperation(value = "通过id获取领料单详情")
    @GetMapping("/getPickingById/{id}")
    public ResultVo getPickingById(@PathVariable(value = "id", required = false) Integer id) {
        if (id != null) {
            return ResultVo.success("操作成功", pickingService.getPickingByID(id));
        } else {
            return ResultVo.fail("请输入ID号");
        }
    }

    @ApiOperation(value = "删除")
    @PutMapping("/delPickingDetails/{id}")
    public ResultVo delPickingDetails(@PathVariable("id") Integer id) {
        UserInfo userInfo = (UserInfo) servletContext.getAttribute("userInfo");
        if(id==null){
            return ResultVo.fail("id不能为空");
        }else {
            pickingDetailsService.update(new UpdateWrapper<CarPickingDetails>()
                    .set("is_delete", 0)
                    .set("creation_by", userInfo.getNameId())
                    .set("creation_time", new Date())
                    .eq("id", id));
        }
        return ResultVo.success("操作成功");
    }
}
